package com.linq.cool.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.api.domain.SysLoginLog;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import com.linq.cool.system.dto.SysLoginLogQueryDTO;
import com.linq.cool.system.service.SysLoginLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/1/29 06:54
 * @Description:
 * @Version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("loginLog")
public class SysLoginLogController extends BaseController {
    @Autowired
    private SysLoginLogService sysLoginLogService;


    @PreAuthorize(hasPerm = "sys:loginLog:list")
    @GetMapping("/list")
    public Result<?> list(SysLoginLogQueryDTO dto) {
        IPage<SysLoginLog> page = sysLoginLogService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }


    @PreAuthorize(hasPerm = "sys:loginLog:remove")
    @Log(moduleTitle = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<?> remove(@PathVariable List<Long> ids) {
        return this.toResult(sysLoginLogService.removeByIds(ids));
    }

    @PreAuthorize(hasPerm = "sys:loginLog:remove")
    @Log(moduleTitle = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public Result<?> clean() {
        sysLoginLogService.clean();
        return Result.ok();
    }


    @PostMapping
    public Result<?> add(@RequestBody SysLoginLog loginLog) {
        return this.toResult(sysLoginLogService.save(loginLog));
    }
}
